package com.tpc.eb.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;


public class BusinessQuartz<T> {

    SchedulerFactory schedulerFactory = null;
    Scheduler scheduler = null ;
    {

        try {
            schedulerFactory = new StdSchedulerFactory();
            scheduler = schedulerFactory.getScheduler();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public void businessJobStart(String params)  throws SchedulerException {
        JSONObject jsonObject = JSON.parseObject(params);
        String businessType = jsonObject.getString("BusinessType");
        String cron = jsonObject.getString("PLAN");
        // 启动scheduler
        scheduler.start();

        String businessJob = businessType + "Job";
        String businessTrigger = businessType + "Trigger";
        // 创建HelloworldJob的JobDetail实例，并设置name/group
        JobDetail jobDetail = JobBuilder.newJob(BusinessJobs.class)
                .withIdentity(businessJob,businessJob)
                //JobDataMap可以给任务传递参数
                .usingJobData("params",params)
                .build();
        // 创建Trigger触发器设置使用cronSchedule方式调度
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(businessTrigger,businessTrigger)
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();
        // 注册JobDetail实例到scheduler以及使用对应的Trigger触发时机
        scheduler.scheduleJob(jobDetail,trigger);
    }

    public void businessJobStop() throws SchedulerException {
        scheduler.shutdown();
    }



    public static void main(String[] args) throws SchedulerException {
        BusinessQuartz businessQuartz = new BusinessQuartz();
//        businessQuartz.businessJobStart(BusinessJobs.class,"AA","0/5 * * * * ?");
    }
}
